package com.mdx.project;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class GenerateClass {
	
	/**
	 * 通过文件名称，文件内容生成文件
	 * @param fileName
	 * @param classContent
	 */
	public static void generateFile (String fileName, String fileContent) {
		
		File file = new File(fileName);
		BufferedOutputStream outputStream = null;
		//如果文件不存在创建文件
		try {
			if(!file.exists()) {
				file.createNewFile();
			}
			outputStream = new BufferedOutputStream(new FileOutputStream(file));
			outputStream.write(fileContent.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			//关闭流文件
			try {
				if(outputStream != null) {
					outputStream.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 生成类型文件
	 */
	public static void generateClass() {
		for (int i = 0; i < PojoConstants.POJO_NAMES.length; i++) {
			for (int j = 0; j < PojoConstants.POJO_TYPE_NAMES.length; j++) {
				String typeLower = firstToLower(PojoConstants.POJO_TYPE_NAMES[j]);
				if("ServiceImpl".equals(PojoConstants.POJO_TYPE_NAMES[j])) {
					typeLower = "service";
				}
				String fileContent = "";
				//文件名
				String fileName = PojoConstants.FILE_PATH + typeLower + "/" + PojoConstants.POJO_NAMES[i] +  PojoConstants.POJO_TYPE_NAMES[j] + PojoConstants.FILE_SUFFIX;
				if("Controller".equals(PojoConstants.POJO_TYPE_NAMES[j])) {
					fileContent = getControllerContent(PojoConstants.POJO_NAMES[i]);
				}
				if("Service".equals(PojoConstants.POJO_TYPE_NAMES[j])){
					fileContent = getServiceContent(PojoConstants.POJO_NAMES[i]);
				}
				if("ServiceImpl".equals(PojoConstants.POJO_TYPE_NAMES[j])){
					fileContent = getServiceImplContent(PojoConstants.POJO_NAMES[i]);
				}
				if("Repository".equals(PojoConstants.POJO_TYPE_NAMES[j])){
					fileContent = getRepositoryContent(PojoConstants.POJO_NAMES[i]);
				}
				generateFile(fileName, fileContent);
			}
		}
	}
	
	public static String getControllerContent(String pojoName) {
		
		StringBuilder fileContent = new StringBuilder();
		String packageStr = "package com.mdx.smarthome.controller;\r\n";
		fileContent.append(packageStr);
		fileContent.append("\r\n");
		fileContent.append("import org.springframework.stereotype.Controller;\r\n");
		fileContent.append("import org.springframework.web.bind.annotation.RequestMapping;\r\n");
		fileContent.append("\r\n");
		fileContent.append("import com.mdx.base.controller.BaseController;\r\n");
		fileContent.append("import com.mdx.smarthome.domain." + pojoName + ";\r\n");
		fileContent.append("\r\n");
		fileContent.append("@Controller\r\n");
		fileContent.append("@RequestMapping(\"/" + pojoName.toLowerCase() + "\")\r\n");
		fileContent.append("public class " + pojoName + "Controller extends BaseController<" + pojoName + ", String> {\r\n");
		fileContent.append("\r\n");
		fileContent.append("	        \r\n");
		fileContent.append("\r\n");
		fileContent.append("}");
		return fileContent.toString();
		
	}
	
	public static String getServiceContent(String pojoName) {
		
		StringBuilder fileContent = new StringBuilder();
		String packageStr = "package com.mdx.smarthome.service;\r\n";
		fileContent.append(packageStr);
		fileContent.append("\r\n");
		fileContent.append("import com.mdx.base.service.BaseService;\r\n");
		fileContent.append("import com.mdx.smarthome.domain." + pojoName + ";\r\n");
		fileContent.append("\r\n");
		fileContent.append("public interface " + pojoName + "Service extends BaseService<" + pojoName + ", String> {\r\n");
		fileContent.append("\r\n");
		fileContent.append("}");
		
		return fileContent.toString();
	}
	
	public static String getServiceImplContent(String pojoName) {
		
		StringBuilder fileContent = new StringBuilder();
		String packageStr = "package com.mdx.smarthome.service;\r\n";
		fileContent.append(packageStr);
		fileContent.append("\r\n");
		fileContent.append("import java.util.List;\r\n");
		fileContent.append("\r\n");
		fileContent.append("import javax.persistence.criteria.CriteriaBuilder;\r\n");
		fileContent.append("import javax.persistence.criteria.Predicate;\r\n");
		fileContent.append("import javax.persistence.criteria.Root;\r\n");
		fileContent.append("\r\n");
		fileContent.append("import org.springframework.data.domain.Sort;\r\n");
		fileContent.append("import org.springframework.stereotype.Service;\r\n");
		fileContent.append("\r\n");
		fileContent.append("import com.mdx.base.service.BaseServiceImpl;\r\n");
		fileContent.append("import com.mdx.base.vo.BaseVo;\r\n");
		fileContent.append("import com.mdx.smarthome.domain." + pojoName + ";\r\n");
		fileContent.append("\r\n");
		fileContent.append("@Service\r\n");
		fileContent.append("public class " + pojoName + "ServiceImpl extends BaseServiceImpl<" + pojoName + ", String> implements " + pojoName + "Service{\r\n");
		fileContent.append("\r\n");
		fileContent.append("	@Override\r\n");
		fileContent.append("	public " + pojoName + " getByCode(String code) {\r\n");
		fileContent.append("		return null;\r\n");
		fileContent.append("	}\r\n");
		fileContent.append("\r\n");		
		fileContent.append("	@Override\r\n");
		fileContent.append("	public List<Predicate> getConditions(BaseVo<" + pojoName + "> baseVo, Root<" + pojoName + "> root, CriteriaBuilder cb) {\r\n");
		fileContent.append("		return null;\r\n");
		fileContent.append("	}\r\n");
		fileContent.append("\r\n");
		fileContent.append("	@Override\r\n");
		fileContent.append("	public Sort getSort() {\r\n");
		fileContent.append("		return null;\r\n");
		fileContent.append("	}\r\n");
		fileContent.append("\r\n");
		fileContent.append("}");
		
		return fileContent.toString();
	}
	
	public static String getRepositoryContent(String pojoName) {
		
		StringBuilder fileContent = new StringBuilder();
		String packageStr = "package com.mdx.smarthome.repository;\r\n";
		fileContent.append(packageStr);
		fileContent.append("\r\n");
		fileContent.append("import com.mdx.base.repository.BaseRepository;\r\n");
		fileContent.append("import com.mdx.smarthome.domain." + pojoName + ";\r\n");
		fileContent.append("\r\n");
		fileContent.append("public interface " + pojoName + "Repository extends BaseRepository<" + pojoName + ", String> {\r\n");
		fileContent.append("\r\n");
		fileContent.append("}");
		
		return fileContent.toString();
	}
	
	public static String firstToLower(String str) {
		str = str.substring(0, 1).toLowerCase() + str.substring(1);
		return str;
	}
	
	public static void main(String[] args) {
		
		generateClass();
		
	}
	
}
